#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif
#include <time.h>
#include <SDL/SDL.h>

int deCube();
int CasesJouer(int x, int y,int z);

int main ( int argc, char** argv )
{
    int done=1;
    int a=1;
    int b=1;
    int c=1;
    int a1=1;
    int e;
    int d;
    int tour;
    int i;
    int de=0;
    int j1=0;
    int j2=0;
    int d0=0;
    int d1=0;
    int j11=0;
    int j22=0;
     SDL_Event event;
    SDL_Event event1;
    SDL_Event event2;
    SDL_Event event3;
    SDL_Event event4;
    SDL_Event eventa;
    SDL_Event eventa1;








    freopen("CON","w",stdout);
    freopen("CON","r",stdin);



 if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )

    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return 1;
    }

    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
                                           SDL_HWSURFACE|SDL_DOUBLEBUF);
    SDL_Surface* bmp = SDL_LoadBMP("acc.bmp");

    SDL_Rect dstrect;
    dstrect.x = (screen->w - bmp->w) / 2;
    dstrect.y = (screen->h - bmp->h) / 2;

    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));


    SDL_BlitSurface(bmp, 0, screen, &dstrect);


    SDL_Flip(screen);
         while(done)
         {
             while(SDL_PollEvent(&event))
                {
                 switch(event.type)
                  {case SDL_MOUSEBUTTONDOWN:
                   {int x=event.motion.x;
                   int y=event.motion.y;

                   if(x<640 && x>323 && y<136 && y>40)
                    {
    SDL_Surface* bmp = SDL_LoadBMP("h1.bmp");

    SDL_Rect dstrect1;
    dstrect1.x = (screen->w - bmp->w) / 2;
    dstrect1.y = (screen->h - bmp->h) / 2;

    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));


    SDL_BlitSurface(bmp, 0, screen, &dstrect1);


    SDL_Flip(screen);
    a=1;
    while (a)
         {while(SDL_PollEvent(&event1))
                {
                 switch(event1.type)
                  {case SDL_KEYDOWN:
                   {

                   if(event1.key.keysym.sym == SDLK_RIGHT)
                    {


    SDL_Surface* bmp = SDL_LoadBMP("h2.bmp");

    SDL_Rect dstrect12;
    dstrect12.x = (screen->w - bmp->w) / 2;
    dstrect12.y = (screen->h - bmp->h) / 2;

    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));


    SDL_BlitSurface(bmp, 0, screen, &dstrect12);


    SDL_Flip(screen);
      b=1;
      while(b)
        {while(SDL_PollEvent(&event2))
                {
                 switch(event2.type)
                  { case SDL_KEYDOWN:
                      {
                          if (event2.key.keysym.sym == SDLK_RIGHT)
                    {


    SDL_Surface* bmp = SDL_LoadBMP("h3.bmp");

    SDL_Rect dstrect3;
    dstrect3.x = (screen->w - bmp->w) / 2;
    dstrect3.y = (screen->h - bmp->h) / 2;

    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));


    SDL_BlitSurface(bmp, 0, screen, &dstrect3);


    SDL_Flip(screen);
    c=0;
    while(c)
        {while(SDL_PollEvent(&event3))
                {
                 switch(event3.type)
                  { case SDL_KEYDOWN:
                      {
                          if (event3.key.keysym.sym == SDLK_RIGHT)
                    {


    SDL_Surface* bmp = SDL_LoadBMP("acc.bmp");

    SDL_Rect dstrect4;
    dstrect4.x = (screen->w - bmp->w) / 2;
    dstrect4.y = (screen->h - bmp->h) / 2;

    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));


    SDL_BlitSurface(bmp, 0, screen, &dstrect4);


    SDL_Flip(screen);
                    }}}}}

                      }
                         if (event2.key.keysym.sym == SDLK_LEFT)
                    {


    SDL_Surface* bmp = SDL_LoadBMP("acc.bmp");

    SDL_Rect dstrect15;
    dstrect15.x = (screen->w - bmp->w) / 2;
    dstrect15.y = (screen->h - bmp->h) / 2;

    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));


    SDL_BlitSurface(bmp, 0, screen, &dstrect15);


    SDL_Flip(screen);
                      }
                      }
                      case SDL_QUIT:
                      b=0;
                   break;

                  }

                  }
                  }
                  }
                    if (event1.key.keysym.sym == SDLK_LEFT)
                    {
                      SDL_Surface* bmp = SDL_LoadBMP("acc.bmp");

    SDL_Rect dstrect11;
    dstrect11.x = (screen->w - bmp->w) / 2;
    dstrect11.y = (screen->h - bmp->h) / 2;

    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));

    SDL_BlitSurface(bmp, 0, screen, &dstrect11);

    SDL_Flip(screen);
                    }
            break;
                   }
            case SDL_QUIT:
                a=0;
                break;

    }}}



                   }
                    if (x<640 && x>281 && y<229 && y>136)
                    {
                        SDL_Surface* bmp = SDL_LoadBMP("ac1.bmp");

    SDL_Rect dstrect2;
    dstrect2.x = (screen->w - bmp->w) / 2;
    dstrect2.y = (screen->h - bmp->h) / 2;

    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));


    SDL_BlitSurface(bmp, 0, screen, &dstrect2);


    SDL_Flip(screen);
    j1=0;
    j2=0;
    a1=1;
    tour=1;

    while(a1)
         {while(SDL_PollEvent(&eventa1))
                {
                 switch(eventa1.type)
                  {case SDL_MOUSEBUTTONDOWN:
                   {int x=eventa1.motion.x;
                   int y=eventa1.motion.y;
                   if(x<640 && x>323 && y<136 && y>40)
                   {de=1;
                if(j1!=63 && j2!=63)
            {
                i=0;
                a=0;
                b=0;
                do
                {
                    j11=j1;
                    d0=deCube();
                    getchar();
                    d1=deCube();
                    getchar();
                    j1=CasesJouer(d0,d1,j1);
                    if(j1==j2)
                    {j1=j22;}
                    i++;
                    if(j1==63)
                    {e=d;}
                    else if (j2==19 && i<=2)
                         {d=2;
                         e=i ; }
                    else if (j2==31)
                         {a=j1;
                         b=31;}
                         else
                         {e=i;
                           d=1;}


                }while(tour==1 && de==1 && e!=d);
                 getchar();
                de=0;
                tour=2;
                i=0;
                e=0;
                d=0;

                do
                {
                    if(j1!=63)
                    {j22=j2;
                        d0=deCube();
                    getchar();
                    d1=deCube();
                    getchar();
                    j2=CasesJouer(d0,d1,j2);
                    if(j2==j1)
                    {j2=j11;}
                    i++;
                    if(j2==63)
                    {e=d;}
                    else if (j1==19 && i==2)
                         {d=2;
                         e=i;}
                    else if (j1==31)
                         {e=j2;
                         b=31;}
                         else
                         { e=i;
                           d=0;}
                }
                }while(tour==2 && de==0 && e!=d);
                tour=1;
                getchar();




            }
            getchar();

                   }
                   break;}

                  }
                }

             }



                   }


                    break;}





               case SDL_QUIT:
                      done=0;
                   break;
                }

         }
         }

    return 0;
}
int deCube()
{ const int MAX = 6, MIN = 1;
int f ;
srand(time(NULL));
f=rand()%((MAX - MIN + 1)) + MIN;
getchar();
return f;
}
int CasesJouer(int x, int y,int z)
{

    if(z==0&&(((x==6) && (y==3))||((x==3)&&(y==6))))
       {z=26;}
    else if(z==0&&(((x==5)&&(y==4))||((x==4)&&(y==5))))
        {z=53;}
        else if ((x+y+z)>63)
        {z-=((x+y+z)-63);}
       else
       {z+=x+y;}

    if(z==6)
     z=12;
    else if(z==42)
    z=30;
    else if(z==58)
    z=0;

    return z;

}
